package com.qf.action;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * //                       .::::.
 * //                     .::::::::.
 * //                    :::::::::::  come on
 * //                 ..:::::::::::'
 * //              '::::::::::::'
 * //                .::::::::::
 * //           '::::::::::::::..
 * //                ..::::::::::::.
 * //              ``::::::::::::::::
 * //               ::::``:::::::::'        .:::.
 * //              ::::'   ':::::'       .::::::::.
 * //            .::::'      ::::     .:::::::'::::.
 * //           .:::'       :::::  .:::::::::' ':::::.
 * //          .::'        :::::.:::::::::'      ':::::.
 * //         .::'         ::::::::::::::'         ``::::.
 * //     ...:::           ::::::::::::'              ``::.
 * //    ```` ':.          ':::::::::'                  ::::..
 * //                       '.:::::'                    ':'````..
 * //
 *
 * @author:小川
 * @date: 2018/11/26 16:34
 */
public class TestPost {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //我们项目的服务端地址
        HttpPost httpPost = new HttpPost("http://localhost:8081/test/post");
        //给服务端传递参数
        List<NameValuePair> parameters=new ArrayList<>();
        parameters.add(new BasicNameValuePair("name","xiaoqiang"));
        parameters.add(new BasicNameValuePair("addrs","zgc"));
        String charset="utf-8";
        HttpEntity entity= new UrlEncodedFormEntity(parameters,charset);
        httpPost.setEntity(entity);
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        //对结果解析
        HttpEntity entity1 = httpResponse.getEntity();
        String s = EntityUtils.toString(entity1, "utf-8");
        System.out.println(s);
        httpClient.close();
    }
}
